package com.caiwangdai.app.backend.observer

import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import com.caiwangdai.app.R
import com.caiwangdai.app.bean.base.BaseResponseB
import com.caiwangdai.app.extension.launchLoginPage
import com.caiwangdai.app.extension.logOut
import com.caiwangdai.app.extension.toast
import com.caiwangdai.app.origin.BaseActivity
import com.caiwangdai.app.widget.ProgressDialogWM
import com.orhanobut.logger.Logger
import io.reactivex.Observer
import io.reactivex.disposables.Disposable

/**
 * @project KotLinProject
 * @author 艹羊
 * @date 2017/6/5 下午6:35
 * @description
 */
abstract class BaseObserver<T : BaseResponseB> : Observer<T> {

    var baseActivity: BaseActivity
    lateinit var disposable: Disposable
    var swipeRefreshLayout: SwipeRefreshLayout? = null


    open fun getProgressBarTitle() = "请稍等"
    open fun getProgressBarContent() = "努力加载中..."
    open fun isShowProgressBar() = true

    constructor(baseActivity: BaseActivity) {
        this.baseActivity = baseActivity

        //progress
        if (!isShowProgressBar()) return
        ProgressDialogWM.showProgressDialog(baseActivity, getProgressBarTitle(), getProgressBarContent())
    }

    constructor(baseActivity: BaseActivity, swipeRefreshLayout: SwipeRefreshLayout, Swipedisposable: Boolean) {
        this.baseActivity = baseActivity
        this.swipeRefreshLayout = swipeRefreshLayout
        swipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(baseActivity, R.color.colorPrimary))
        //是否为一次性
        if (Swipedisposable) {
            swipeRefreshLayout.isEnabled = false
            return
        }
        swipeRefreshLayout.setOnRefreshListener {
            onSwipeRefresh()
        }
    }

    override fun onSubscribe(d: Disposable?) {
        this.disposable = d!!
        baseActivity.addDisposable(disposable)
        beginRefreshAnimation()
    }

    override fun onComplete() {
        ProgressDialogWM.hideProgressDialog()
        endRefreshAnimation()
    }

    override fun onError(e: Throwable?) {
        Logger.e("错误 " + e.toString())
        ProgressDialogWM.hideProgressDialog()
        onFail(e.toString())
    }

    override fun onNext(value: T) {
        Logger.json(value.toJson())
        if (value.code == 200) {
            onSuccess(value)
        } else {
            toast(value.message + "")
            //特殊处理
            if (value.code == 500 && value.message == "用户未登录，请登录") {
                baseActivity.logOut()
                baseActivity.launchLoginPage()
            } else {
                onFail(value.message)
            }
        }
    }

    //下拉刷新
    open fun onSwipeRefresh() {}

    open fun onSuccess(value: T) {}

    open fun onFail(message: String?) {}


    private fun beginRefreshAnimation() {
        swipeRefreshLayout?.post({ swipeRefreshLayout!!.isRefreshing = true })
    }

    private fun endRefreshAnimation() {
        swipeRefreshLayout?.postDelayed({ swipeRefreshLayout!!.isRefreshing = false }, 1000)
    }


}